/**
* @file data.vue
* @author huang
* @date 2023/8/22
* @description 字典数据
* @projectName
* @email newflyhuang@gmail.com
*/
<template>

  <pure-table
    ref="pureTable"
    :hide-index="true"
    :list-api="listApi"
    :query-condition="queryCondition"
    :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    row-key="dictCode"
    selection
    @selection-change="handleSelectionChange"
  >
    <template #header>
      <query-panel @query="handleQuery" @reset="resetQuery">
        <el-form-item prop="dictType">
          <el-select v-model="queryParams.dictType" @change="changeType">
            <el-option
              v-for="item in typeOptions"
              :key="item.dictId"
              :label="item.dictName"
              :value="item.dictType"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="dictLabel">
          <el-input
            v-model="queryParams.dictLabel"
            clearable
            placeholder="请输入字典标签"
          />
        </el-form-item>
        <el-form-item prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="数据状态" />
        </el-form-item>
        <template #extra>
          <pure-button
            icon="add"
            label="新增"
            plain
            @click="handleAdd"
          />
          <pure-button
            :disabled="multiple"
            icon="delete"
            label="删除"
            @click="handleDelete"
          />
          <pure-button
            icon="close"
            label="关闭"
            plain
            type="patch"
            @click="handleClose"
          />
        </template>
      </query-panel>
    </template>
    <el-table-column align="center" label="字典编码" prop="dictCode" />
    <el-table-column align="center" label="字典标签" prop="dictLabel" />
    <el-table-column align="center" label="字典键值" prop="dictValue" />
    <el-table-column align="center" label="字典排序" prop="dictSort" />
    <el-table-column align="center" label="状态" prop="status">

      <template #default="{row}">
        {{ row.status == '0' ? '正常' : '停用' }}
      </template>

    </el-table-column>
    <el-table-column :show-overflow-tooltip="true" align="center" label="备注" prop="remark" />
    <el-table-column align="center" label="创建时间" prop="createTime" width="180" />
    <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
      <template slot-scope="scope">
        <pure-button
          label="修改"
          size="mini"
          type="text"
          @click="handleUpdate(scope.row)"
        />
        <pure-button
          icon="el-icon-delete"
          label="删除"
          type="text"
          @click="handleDelete(scope.row)"
        />
      </template>
    </el-table-column>
  </pure-table>

</template>

<script>
import SystemDataDictionaryApi from './api';
import QueryPanel from '@/common/components/query/queryPanel.vue';
import DialogAddDicData from './dialog/addDicData.vue';
import { convertToTree, } from '@/common/utils/convert';

export default {
  name: 'Data',
  components: { QueryPanel, },
  data() {
    return {
      listApi: SystemDataDictionaryApi.getDictionaryDataList,

      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 默认字典类型
      defaultDictType: '',
      // 类型数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        dictName: undefined,
        dictType: undefined,
        status: undefined,
      },
      queryCondition: {},
      dicInfo: null,
    };
  },
  created() {
    // this.queryParams.dictType = this.$route.params && this.$route.params.dictId;
    this.getTypeList();
    this.getType();
  },
  mounted() {
    this.$refs.pureTable.convertListData = this.convertListData;
  },
  methods: {
    convertListData(data) {
      return convertToTree(data, {
        id: 'dictCode',
        parentId: 'parentId',
        children: 'children',
      });
    },
    /** 查询字典类型详细 */
    async getType() {
      const dictId = this.$route.params && this.$route.params.dictId;

      const rs = await SystemDataDictionaryApi.getDictionaryDetail(dictId);
      this.dicInfo = rs.data;
      this.queryParams.dictType = rs.data.dictType;
      this.defaultDictType = rs.data.dictType;
      this.queryCondition = {
        ...this.queryParams,
      };
    },
    /** 查询字典类型列表 */
    async getTypeList() {
      const rs = await SystemDataDictionaryApi.getDictionaryTypeList();
      this.typeOptions = rs.data;
    },
    // 字典类型选中事件
    changeType(value) {
      this.queryParams.dictType = value;
      this.queryCondition = {
        ...this.queryParams,
      };
    },
    // 表单重置
    reset() {
      this.form = {
        dictCode: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        cssClass: undefined,
        listClass: 'default',
        dictSort: 0,
        status: '0',
        remark: undefined,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryCondition = {
        ...this.queryParams,
      };
      this.$refs.pureTable.fetchData();
    },
    /** 返回按钮操作 */
    handleClose() {
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        dictLabel: '',
        dictType: '',
        status: '',
      };
      this.queryParams.dictType = this.defaultDictType;
      this.$refs.pureTable.fetchData();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$dialog({
        component: () => <DialogAddDicData dictType={this.queryParams.dictType}
          dictLevel={this.dicInfo.dictLevel}
          onDone={this.onDone}
        />,
      });
    },
    onDone() {
      this.$refs.pureTable.fetchData();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictCode);
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$dialog({
        component: () => <DialogAddDicData data={row}
          tilte={'修改数据'}
          dictType={this.queryParams.dictType}
          onDone={this.onDone}
        />,
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const dictCodes = row.dictCode || this.ids;
      this.$confirm('是否确认删除字典编码为"' + dictCodes + '"的数据项？').then(async () => {
        await SystemDataDictionaryApi.deleteDictionaryData(dictCodes);
        this.onDone();
        this.$message.success('删除成功');
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/data/export', {
        ...this.queryParams,
      }, `data_${new Date().getTime()}.xlsx`);
    },
  },
};
</script>
